package com.yc.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.config.AiConfig;
import com.yc.web.Controller.moderl.ResponseResult;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.service.TokenStream;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Sinks;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@RestController
public class deepseekController {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(deepseekController.class);
    @Autowired
    public AiConfig.AiAssistant assitant;

    @GetMapping(value = "/api/deepseekchat", produces = MediaType.TEXT_PLAIN_VALUE)  // 改为TEXT_PLAIN
    public Flux<String> chatStream(
            @RequestParam("memoryId") String memoryId,
            @RequestParam("question") String question) {
        TokenStream tokenStream = assitant.chatdeepseekStream(memoryId, question);
        return Flux.create(fluxSink -> {
            tokenStream.onPartialResponse(response -> {
                //System.out.println(response);
                fluxSink.next(response);  // 直接发送内容，不包装在data:中
            }).onCompleteResponse(response -> {
                //System.out.println("完成");
                fluxSink.next("\n[DONE]");
                fluxSink.complete();
            }).onError(error -> {
                fluxSink.error(error);
            }).start();
        }, FluxSink.OverflowStrategy.BUFFER);
    }
}

